package com.chima.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.chima.domain.DetailPost;
import com.chima.domain.Post;
import com.chima.domain.Professional;

public class ProfessionalDao extends HibernateDaoSupport {
	//分页查询
	public List<Professional> findByCriteria(DetachedCriteria criteria, int firstResult,int maxResults) {
		return this.getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults);
	}
   
	//查找总页数
//	public int findByTotalCount() {
//		List list= this.getHibernateTemplate().find("select count(*) from Professional");
//		long temp = (Long) list.get(0);
//		return (int) temp;
//	}
	public int findByTotalCount(int depart) {
		
		long temp=231231l;
		if(depart==0){
			List list= this.getHibernateTemplate().find("select count(*) from Professional");
			temp = (Long) list.get(0);
		}else{
			List list= this.getHibernateTemplate().find("select count(*) from Professional p where p.department.id='"+depart+"' ");
			 temp = (Long) list.get(0);
		}
		return (int) temp;
	}
	public void save(Professional professional) {
		this.getHibernateTemplate().save(professional);
	}
	public void update(Professional professional) {
		this.getHibernateTemplate().update(professional);
	}
	public void delete(int id) {
		this.getHibernateTemplate().delete(this.getHibernateTemplate().load(Professional.class, id));
	}
	public Professional findByDepartAndName(int depart,int id) {
		
		Session session=this.getSession();
		Professional p=(Professional) session.createQuery(" from Professional where department.id=? and id=?").setParameter(0, depart).setParameter(1,id).uniqueResult();
		super.releaseSession(session);
		return p;
	}
	public List<Professional> findByDepart(int depart) {
		Session session=this.getSession();
		List<Professional> p= session.createQuery(" from Professional where department.id=?").setParameter(0, depart).list();
		super.releaseSession(session);
		return p;
	}
	public List<Professional> ListAllProfessional() {
			
			Session session=this.getSession();
			List<Professional> p= session.createQuery(" from Professional ").list();
			super.releaseSession(session);
			return p;
		
		}
	public Professional findById(int id) {
		
		Session session=this.getSession();
		Professional p= (Professional) session.createQuery(" from Professional where id=?").setParameter(0, id).uniqueResult();
		super.releaseSession(session);
		return p;
	}
	public Professional findByName(String name) {
		
		Session session=this.getSession();
		Professional p= (Professional) session.createQuery(" from Professional where name=?").setParameter(0, name).uniqueResult();
		super.releaseSession(session);
		return p;
	}

}
